package org.i1510.common.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.i1510.common.bean.PageBean;
import org.i1510.common.dao.ISysQuartzDao;
import org.i1510.common.po.SysQuartz;
import org.i1510.common.service.ISysQuartzService;
import org.i1510.common.utils.BeanUtils;
import org.i1510.common.utils.DateUtils;
import org.i1510.common.utils.IDGenerator;
import org.i1510.common.utils.SpringContextUtil;
import org.i1510.common.utils.UserUtils;
import org.i1510.common.utils.quartz.QuartzHandler;
import org.i1510.common.utils.web.ResultMapper;
import org.quartz.Scheduler;
import org.quartz.impl.StdScheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service("iSysQuartzService")
public class SysQuartzServiceImpl extends BaseServiceImpl implements ISysQuartzService {

	private static final Logger logger = LoggerFactory.getLogger(SysQuartzServiceImpl.class);
	@Autowired
	private ISysQuartzDao iSysQuartzDao;	
	
	public ResultMapper save(SysQuartz basePo,HttpServletRequest request) {

		ResultMapper mapper = ResultMapper.generateFailure();
		SysQuartz saved = null;
		if(null !=basePo && basePo.getDocId() != null  && !basePo.getDocId().equals("")){//修改
			SysQuartz finder = iSysQuartzDao.findById(basePo.getDocId());   
			if(null != finder){
				finder.setDocLastTime(DateUtils.getNow());
				finder.setDocLastor(UserUtils.getSessionUserBean(request).getSysUser().getDocId());			
			    BeanUtils.copy(finder, basePo);
			    saved = iSysQuartzDao.save(finder);
			}else{
				mapper = ResultMapper.generateFailure();
				return mapper;
			}
			
		}else{//新增
			basePo.setDocId(IDGenerator.getGenerateID());
			basePo.setDocCreateTime(DateUtils.getNow());
			basePo.setDocCreator(UserUtils.getSessionUserBean(request).getSysUser().getDocId());
			saved = iSysQuartzDao.save(basePo);
		}

		if (saved == null) {
			mapper = ResultMapper.generateFailure();
			return mapper;
		}else{
			mapper = ResultMapper.generateSuccess();
			return mapper;			
		}
	}
	public ResultMapper delete(String docId) {

		ResultMapper mapper = ResultMapper.generateFailure();
		iSysQuartzDao.delete(docId);
		mapper = ResultMapper.generateSuccess();
		return mapper;
	}

	
	public SysQuartz findById(String docId){
		return  iSysQuartzDao.findById(docId);
	}
	
	public List<SysQuartz> findByIds(List<String> docIds){
		return (List<SysQuartz>) iSysQuartzDao.findAll(docIds);
	}
	public List getPage(SysQuartz sysQuartz,PageBean pageBean,Map conditions) {
		Map map = new HashMap();
		//Bean转成Map
		BeanUtils.ObjectCopyMap(sysQuartz, map);
		map.put("intPageIndex", pageBean.getIntPageIndex());
		map.put("intPageSize", pageBean.getIntPageSize());
		map.putAll(conditions);
		List list = this.queryForListBySql("sysQuartzSql.getPage", map);
		return list;
	}
	public int getPageCount(SysQuartz sysQuartz,Map conditions) {
		Map map = new HashMap();
		//Bean转成Map
		BeanUtils.ObjectCopyMap(sysQuartz, map);
		map.putAll(conditions);
		List list = this.queryForListBySql("sysQuartzSql.getPageCount", map);		
		return list.size();
	}
 
	/**
	*@Author Rambo
	*@Desc：暂停任务
	*@param taskName
	*@param group TODO
	*@return TODO
	*@Version  ISysQuartzService.java,v 1.1 2013-10-11 下午12:22:23 
	*/
	public ResultMapper pause(String taskName,String group){
		StdScheduler schedulerFactory = (StdScheduler)SpringContextUtil.getBean("schedulerFactory");		
		QuartzHandler quartzHandler = new QuartzHandler(schedulerFactory);
		quartzHandler.pause(taskName, group);
		
		ResultMapper mapper = ResultMapper.generateSuccess();
		mapper.appendResult("taskName", taskName);
		return mapper;
	}
	
	/**
	*@Author Rambo
	*@Desc：开启任务
	*@param taskName
	*@param group TODO
	*@return TODO
	*@Version  SysQuartzServiceImpl.java,v 1.1 2013-10-11 下午12:23:42 
	*/
	public ResultMapper resume(String taskName,String group){
		StdScheduler schedulerFactory = (StdScheduler)SpringContextUtil.getBean("schedulerFactory");		
		QuartzHandler quartzHandler = new QuartzHandler(schedulerFactory);
		quartzHandler.resume(taskName, group);
		ResultMapper mapper = ResultMapper.generateFailure();
		mapper = ResultMapper.generateSuccess();
		mapper.appendResult("taskName", taskName);
		return mapper;
	}
//	public void printOut
	
	public void printOut(){

		
//		StdScheduler schedulerFactory = (StdScheduler)SpringContextUtil.getBean("schedulerFactory");
//		
//		QuartzHandler quartzHandler = new QuartzHandler(schedulerFactory);
//		
//		String expression = "0/15 * * ? * *";
//		
//		System.out.println(new Date()+"---修改定时任务表达式为 0/15 * * ? * *-----");
//		quartzHandler.updateCronExpression("doTime", Scheduler.DEFAULT_GROUP, expression);
//		
//		System.out.println(new Date()+"---暂停定时计划 20秒-----");
//		quartzHandler.standBy();
//		System.out.println(new Date()+"---开启计划-----");
//		quartzHandler.start();
//		
//		
//		System.out.println(new Date()+"---暂停定时任务 25秒-----");
//		quartzHandler.pause("doTime", Scheduler.DEFAULT_GROUP);
//		System.out.println(new Date()+"---开启任务-----");
//		quartzHandler.resume("doTime", Scheduler.DEFAULT_GROUP);
//		
//		
//		Timer executeSchedule = new Timer();  
//	    executeSchedule.schedule(new TimerTask() {  
//	      @Override  
//	      public void run() {  
//	        System.out.println("--暂停30秒--！");  
//	      }  
//	    }, 0, 5000);  
//	  
//		quartzHandler.unDeploy("doTime", Scheduler.DEFAULT_GROUP);
//		System.out.println(new Date()+"---删除任务-----");
		
		System.out.println(new Date()+":show quartz!");
	}
	
	

	
}
